import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('网络请求'),
        ),
        body: WeatherInfo(),
      ),
    );
  }
}

// http获取网络数据
class WeatherInfo extends StatefulWidget {
  @override
  _WeatherInfoState createState() => _WeatherInfoState();
}

class _WeatherInfoState extends State<WeatherInfo> {
  String _response = "";
  WeatherData weatherData;

  // 是否正在获取数据，避免重复请求
  bool isLoading = false;

  refresh() async {
    if (!isLoading) {
      try {
        HttpClient client = HttpClient();
        HttpClientRequest request = await client
            .getUrl(Uri.parse('http://weather.com.cn/data/sk/101110101.html'));
        HttpClientResponse response = await request.close();
        String result = await response.transform(utf8.decoder).join();
        setState(() {
          _response = result;
          weatherData = WeatherData.formJson(json.decode(result));
        });
        client.close();
      } catch (e) {
        setState(() {
          _response = "请求失败$e";
        });
      } finally {
        setState(() {
          isLoading = false;
        });
      }
    } else {
      setState(() {
        _response = "正在获取数据";
      });
    }
  }

  @override
  void initState() {
    super.initState();
    refresh();
  }

  Column _buildWeatherInfo(WeatherData weatherData) {
    List<Widget> widgets = List();
    WeatherInfoData weatherInfoData = weatherData.weatherinfo;
    widgets.add(Text('city:${weatherInfoData.city}'));
    widgets.add(Text('city:${weatherInfoData.time}'));
    widgets.add(Text('SD:${weatherInfoData.SD}'));
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: widgets,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child:
          weatherData == null ? Text('正在获取') : _buildWeatherInfo(weatherData),
    );
  }
}

class WeatherData {
  WeatherInfoData weatherinfo;

  WeatherData(this.weatherinfo);

  WeatherData.formJson(Map<String, dynamic> jsonStr) {
    this.weatherinfo = WeatherInfoData.fromJson(jsonStr['weatherinfo']);
  }
}

class WeatherInfoData {
  String city;
  String cityid;
  String temp;
  String WD;
  String WS;
  String SD;
  String AP;
  String njd;
  String WSE;
  String time;
  String sm;
  String Radar;

  WeatherInfoData(this.city, this.cityid, this.temp, this.WD, this.WS, this.SD,
      this.AP, this.njd, this.WSE, this.time, this.sm, this.Radar);

  WeatherInfoData.fromJson(Map<String, dynamic> jsonStr) {
    this.city = jsonStr['city'];
    this.cityid = jsonStr['cityid'];
    this.temp = jsonStr['temp'];
    this.WD = jsonStr['WD'];
    this.WS = jsonStr['WS'];
    this.SD = jsonStr['SD'];
    this.AP = jsonStr['AP'];
    this.njd = jsonStr['njd'];
    this.WSE = jsonStr['WSE'];
    this.time = jsonStr['time'];
    this.sm = jsonStr['sm'];
    this.Radar = jsonStr['Radar'];
  }
}
